package autotest.web.framework.plugin.wizards;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;

public class NewPageWizard extends NewWizard
{
	private NewPageWizardClassPage newPageWizardClassPage;
	private NewPageWizardFieldPage newPageWizardFieldPage;
	
	@Override
	public void addPages()
	{
		if(newPageWizardClassPage == null)
		{
			newPageWizardClassPage = new NewPageWizardClassPage();
		}
		addPage(newPageWizardClassPage);
		
		if(newPageWizardFieldPage == null)
		{
			newPageWizardFieldPage = new NewPageWizardFieldPage("");
		}
		addPage(newPageWizardFieldPage);
		
		newPageWizardClassPage.init(currentSelection);
		super.addPages();
	}

	@Override
	public boolean performFinish()
	{
		try
		{
			getContainer().run(true, false, new FinishProgress());
			
			return true;
		}
		catch (InvocationTargetException | InterruptedException e)
		{
			e.printStackTrace();
		}
		
		return false;
	}
	
	class FinishProgress implements IRunnableWithProgress
	{

		@Override
		public void run(IProgressMonitor monitor)
				throws InvocationTargetException, InterruptedException
		{
			try
			{
				newPageWizardClassPage.setPageFieldMap(newPageWizardFieldPage.getPageFieldMap());
				newPageWizardClassPage.createType(monitor);
			}
			catch (CoreException e)
			{
				e.printStackTrace();
			}
		}
	}
}